package com.grouuu.world;

import com.grouuu.layer.LayerGround;
import haxe.Timer;
import js.Browser;
import js.Error;
import js.html.KeyboardEvent;
import js.html.RequestAnimationFrameCallback;

/**
 * ...
 * @author Grouuu
 */
class World
{
	static public inline var FPS:Int = 60;
	static public inline var NB_COL:Int = 19;
	static public inline var NB_ROW:Int = 25;
	static public inline var SCENE_W:Int = 608;
	static public inline var SCENE_H:Int = 800;
	static public inline var TILE_W:Int = 32;
	static public inline var TILE_H:Int = 32;
	
	private var layerGround:LayerGround;
	
	private var current_x:Int;
	private var current_y:Int;
	private var timer:Timer;
	private var isRaf:Bool; // si Browser.window.requestAnimationFrame est supporté
	private var idRaf:Int;
	
	/**
	 * if(newTiles)
	 * 		buffer clear old zones
	 * 		buffer add new zones
	 * 		buffer translate to origin
	 * screen clear
	 * screen copy buffer
	 */

	public function new() 
	{
		init();
	}
	
	private function init():Void
	{
		current_x = 0;
		current_y = 0;
		
		isRaf = (Browser.window.requestAnimationFrame != null);
		
		isRaf = false;
		
		if (!isRaf)
			timer = new Timer(Std.int(1000 / FPS));
	}
	
	public function addLayer(layer:Dynamic):Void
	{
		if (Std.is(layer, LayerGround))
			layerGround = layer;
	}
	
	public function start():Void
	{
		layerGround.init(0, 0);
		
		Browser.document.onkeydown = onKeyDown;
		Browser.document.onkeypress = onKeyDown;
		Browser.document.onkeyup = onKeyUp;
		
		if (isRaf)
			updateRaf();
		else
			timer.run = updateFallback;
		
		/**
		 * (function()
		 * {
				var lastTime = 0;
				var vendors = ['webkit', 'moz'];
				for(var x = 0; x < vendors.length && !window.requestAnimationFrame; ++x)
				{
					window.requestAnimationFrame = window[vendors[x]+'RequestAnimationFrame'];
					window.cancelAnimationFrame =
					  window[vendors[x]+'CancelAnimationFrame'] || window[vendors[x]+'CancelRequestAnimationFrame'];
				}
				
				if (!window.requestAnimationFrame)
					window.requestAnimationFrame = function(callback, element)
					{
						var currTime = new Date().getTime();
						var timeToCall = Math.max(0, 16 - (currTime - lastTime));
						var id = window.setTimeout(function() { callback(currTime + timeToCall); },
						  timeToCall);
						lastTime = currTime + timeToCall;
						return id;
					};
					
				if (!window.cancelAnimationFrame)
					window.cancelAnimationFrame = function(id)
					{
						clearTimeout(id);
					};
			}());
		 */
	}
	
	public function stop():Void
	{
		if (isRaf)
		{
			try
			{
				Browser.window.cancelAnimationFrame(idRaf); // Chrome
			}
			catch (error:Error)
			{
				Browser.window.cancelRequestAnimationFrame(idRaf); // Firefox
			}
		}
		else
			timer.stop();
	}
	
	private function updateRaf(?id:Float):Bool
	{
		idRaf = Browser.window.requestAnimationFrame(updateRaf);
		
		update();
		
		return true;
	}
	
	private function updateFallback():Void
	{
		update();
	}
	
	private function update():Void
	{
		/*current_x += 10;
		
		if (current_x > 10 * 32)
			stop();*/
		
		if (currentKey == 90 || currentKey == 38) // Z or ↑
			current_y--;
		else if (currentKey == 83 || currentKey == 40) // S or ↓
			current_y++;
		else if (currentKey == 81 || currentKey == 37) // Q or ←
			current_x--;
		else if (currentKey == 68 || currentKey == 39) // D or →
			current_x++;
		
		if (current_x < 0)
			current_x = 0;
		
		if (current_y < 0)
			current_y = 0;
		
		trace("UPDATE");
		if (currentKey != 0)
			trace("MOVE");
		
		layerGround.update(current_x, current_y);
	}
	
	private var currentKey:Int = 0;
	
	private function onKeyUp(e:KeyboardEvent):Void
	{
		e.preventDefault();
		
		currentKey = 0;
	}
	
	private function onKeyDown(e:KeyboardEvent):Void
	{
		e.preventDefault();
		
		var keyCode:Int = e.keyCode;
		
		if (keyCode != 0)
			currentKey = e.keyCode;
	}
}